Rewrite registry tests for easier mocking and such
authorAlex Crichton <alex@alexcrichton.com>
Thu, 23 Oct 2014 05:05:30 +0000 (22:05 -0700)
committerAlex Crichton <alex@alexcrichton.com>
Mon, 27 Oct 2014 19:40:23 +0000 (12:40 -0700)
tests/fixtures/bar-0.0.1.tar.gz [deleted file]
tests/fixtures/foo-0.0.1.tar.gz [deleted file]
tests/fixtures/hello.rs [deleted file]
tests/fixtures/notyet-0.0.1.tar.gz [deleted file]
tests/support/mod.rs
tests/support/registry.rs [new file with mode: 0644]
tests/test_cargo_registry.rs

diff --git a/tests/fixtures/bar-0.0.1.tar.gz b/tests/fixtures/bar-0.0.1.tar.gz
deleted file mode 100644 (file)
index 55b2f5a..0000000
Binary files a/tests/fixtures/bar-0.0.1.tar.gz and /dev/null differ
diff --git a/tests/fixtures/foo-0.0.1.tar.gz b/tests/fixtures/foo-0.0.1.tar.gz
deleted file mode 100644 (file)
index bf1fc95..0000000
Binary files a/tests/fixtures/foo-0.0.1.tar.gz and /dev/null differ
diff --git a/tests/fixtures/hello.rs b/tests/fixtures/hello.rs
deleted file mode 100644 (file)
index e7a694f..0000000
+++ /dev/null
@@ -1,3 +0,0 @@
-fn main() {
-  println!("WINNING");
-}
diff --git a/tests/fixtures/notyet-0.0.1.tar.gz b/tests/fixtures/notyet-0.0.1.tar.gz
deleted file mode 100644 (file)
index 5b8ae93..0000000
Binary files a/tests/fixtures/notyet-0.0.1.tar.gz and /dev/null differ
index 8036060b17076a265793da0b1be52c474a2bc7ce..cb8f1644370d0c34f48ea60f8e290311bd79cca4 100644 (file)
@@ -15,6 +15,7 @@ use support::paths::PathExt;
 
 pub mod paths;
 pub mod git;
+pub mod registry;
 
 /*
  *
diff --git a/tests/support/registry.rs b/tests/support/registry.rs
new file mode 100644 (file)
index 0000000..c25953e
--- /dev/null
@@ -0,0 +1,124 @@
+use std::io::{mod, fs, File};
+
+use flate2::Default;
+use flate2::writer::GzEncoder;
+use git2;
+use serialize::hex::ToHex;
+use tar::Archive;
+use url::Url;
+
+use support::{ResultTest, project};
+use support::paths;
+use support::git::repo;
+use cargo::util::Sha256;
+
+pub fn registry_path() -> Path { paths::root().join("registry") }
+pub fn registry() -> Url { Url::from_file_path(&registry_path()).unwrap() }
+pub fn dl_path() -> Path { paths::root().join("dl") }
+pub fn dl_url() -> Url { Url::from_file_path(&dl_path()).unwrap() }
+
+pub fn init() {
+    let config = paths::root().join(".cargo/config");
+    fs::mkdir_recursive(&config.dir_path(), io::USER_DIR).assert();
+    File::create(&config).write_str(format!(r#"
+        [registry]
+            index = "{reg}"
+            token = "api-token"
+    "#, reg = registry()).as_slice()).assert();
+
+    // Init a new registry
+    repo(&registry_path())
+        .file("config.json", format!(r#"
+            {{"dl":"{}","api":""}}
+        "#, dl_url()).as_slice())
+        .build();
+}
+
+pub fn mock_archive(name: &str, version: &str, deps: &[(&str, &str)]) {
+    let mut manifest = format!(r#"
+        [package]
+        name = "{}"
+        version = "{}"
+        authors = []
+    "#, name, version);
+    for &(dep, req) in deps.iter() {
+        manifest.push_str(format!(r#"
+            [dependencies.{}]
+            version = "{}"
+        "#, dep, req).as_slice());
+    }
+    let p = project(name)
+        .file("Cargo.toml", manifest.as_slice())
+        .file("src/lib.rs", "");
+    p.build();
+
+    let dst = mock_archive_dst(name, version);
+    fs::mkdir_recursive(&dst.dir_path(), io::USER_DIR).assert();
+    let f = File::create(&dst).unwrap();
+    let a = Archive::new(GzEncoder::new(f, Default));
+    a.append(format!("{}-{}/Cargo.toml", name, version).as_slice(),
+             &mut File::open(&p.root().join("Cargo.toml")).unwrap()).unwrap();
+    a.append(format!("{}-{}/src/lib.rs", name, version).as_slice(),
+             &mut File::open(&p.root().join("src/lib.rs")).unwrap()).unwrap();
+    a.finish().unwrap();
+}
+
+pub fn mock_archive_dst(name: &str, version: &str) -> Path {
+    dl_path().join(name).join(version).join("download")
+}
+
+pub fn mock_pkg(name: &str, version: &str, deps: &[(&str, &str)]) {
+    mock_archive(name, version, deps);
+    let c = File::open(&mock_archive_dst(name, version)).read_to_end().unwrap();
+    let line = pkg(name, version, deps, cksum(c.as_slice()).as_slice());
+
+    let file = match name.len() {
+        1 => format!("1/{}", name),
+        2 => format!("2/{}", name),
+        3 => format!("3/{}/{}", name.slice_to(1), name),
+        _ => format!("{}/{}/{}", name.slice(0, 2), name.slice(2, 4), name),
+    };
+    publish(file.as_slice(), line.as_slice());
+}
+
+pub fn publish(file: &str, line: &str) {
+    let repo = git2::Repository::open(&registry_path()).unwrap();
+    let mut index = repo.index().unwrap();
+    {
+        let dst = registry_path().join(file);
+        let prev = File::open(&dst).read_to_string().unwrap_or(String::new());
+        fs::mkdir_recursive(&dst.dir_path(), io::USER_DIR).unwrap();
+        File::create(&dst).write_str((prev + line + "\n").as_slice()).unwrap();
+    }
+    index.add_path(&Path::new(file)).unwrap();
+    index.write().unwrap();
+    let id = index.write_tree().unwrap();
+    let tree = repo.find_tree(id).unwrap();
+    let sig = repo.signature().unwrap();
+    let parent = repo.refname_to_id("refs/heads/master").unwrap();
+    let parent = repo.find_commit(parent).unwrap();
+    repo.commit(Some("HEAD"), &sig, &sig,
+                "Another commit", &tree,
+                [&parent]).unwrap();
+}
+
+pub fn pkg(name: &str, vers: &str, deps: &[(&str, &str)], cksum: &str) -> String {
+    let deps = deps.iter().map(|&(a, b)| dep(a, b)).collect::<Vec<String>>();
+    format!(r#"{{"name":"{}","vers":"{}","deps":{},"cksum":"{}","features":{{}}}}"#,
+            name, vers, deps, cksum)
+}
+
+pub fn dep(name: &str, req: &str) -> String {
+    format!("{{\"name\":\"{}\",\
+               \"req\":\"{}\",\
+               \"features\":[],\
+               \"default_features\":false,\
+               \"target\":null,\
+               \"optional\":false}}", name, req)
+}
+
+pub fn cksum(s: &[u8]) -> String {
+    let mut sha = Sha256::new();
+    sha.update(s);
+    sha.finish().to_hex()
+}
index b5fe5bc8b28ccadb8c63d4e27e95fb0e4207b14d..c77b993a2ade2bbb6a9478fe8bdc16a1b6418ddc 100644 (file)
@@ -1,87 +1,13 @@
-use std::io::{mod, fs, File};
-use url::Url;
-use git2;
-use serialize::hex::ToHex;
+use std::io::File;
 
-use support::{ResultTest, project, execs, cargo_dir};
+use support::{project, execs, cargo_dir};
 use support::{UPDATING, DOWNLOADING, COMPILING, PACKAGING, VERIFYING};
-use support::paths;
-use support::git::repo;
-use cargo::util::Sha256;
+use support::registry as r;
 
 use hamcrest::assert_that;
 
-fn registry_path() -> Path { paths::root().join("registry") }
-fn registry() -> Url { Url::from_file_path(&registry_path()).unwrap() }
-fn dl_path() -> Path { paths::root().join("dl") }
-fn dl_url() -> Url { Url::from_file_path(&dl_path()).unwrap() }
-
-fn cksum(s: &[u8]) -> String {
-    let mut sha = Sha256::new();
-    sha.update(s);
-    sha.finish().to_hex()
-}
-
 fn setup() {
-    let config = paths::root().join(".cargo/config");
-    fs::mkdir_recursive(&config.dir_path(), io::USER_DIR).assert();
-    File::create(&config).write_str(format!(r#"
-        [registry]
-            index = "{reg}"
-            token = "api-token"
-    "#, reg = registry()).as_slice()).assert();
-
-    // Prepare the "to download" artifacts
-    let foo = include_bin!("fixtures/foo-0.0.1.tar.gz");
-    let bar = include_bin!("fixtures/bar-0.0.1.tar.gz");
-    let notyet = include_bin!("fixtures/notyet-0.0.1.tar.gz");
-    let foo_cksum = dl("foo", "0.0.1", foo);
-    let bar_cksum = dl("bar", "0.0.1", bar);
-    dl("bad-cksum", "0.0.1", foo);
-    let notyet = dl("notyet", "0.0.1", notyet);
-
-    // Init a new registry
-    repo(&registry_path())
-        .file("config.json", format!(r#"
-            {{"dl":"{}","api":""}}
-        "#, dl_url()).as_slice())
-        .file("3/f/foo", pkg("foo", "0.0.1", [], &foo_cksum))
-        .file("3/b/bar", pkg("bar", "0.0.1", [
-            "{\"name\":\"foo\",\
-              \"req\":\">=0.0.0\",\
-              \"features\":[],\
-              \"default_features\":false,\
-              \"target\":null,\
-              \"optional\":false}"
-        ], &bar_cksum))
-        .file("ba/d-/bad-cksum", pkg("bad-cksum", "0.0.1", [], &bar_cksum))
-        .nocommit_file("no/ty/notyet", pkg("notyet", "0.0.1", [], &notyet))
-        .build();
-
-    fn pkg(name: &str, vers: &str, deps: &[&str], cksum: &String) -> String {
-        format!(r#"{{"name":"{}","vers":"{}","deps":{},"cksum":"{}","features":{{}}}}"#,
-                name, vers, deps, cksum)
-    }
-    fn dl(name: &str, vers: &str, contents: &[u8]) -> String {
-        let dst = dl_path().join(name).join(vers).join("download");
-        fs::mkdir_recursive(&dst.dir_path(), io::USER_DIR).assert();
-        File::create(&dst).write(contents).unwrap();
-        cksum(contents)
-    }
-}
-
-fn publish_notyet() {
-    let repo = git2::Repository::open(&registry_path()).unwrap();
-    let mut index = repo.index().unwrap();
-    index.add_path(&Path::new("no/ty/notyet")).unwrap();
-    let id = index.write_tree().unwrap();
-    let tree = repo.find_tree(id).unwrap();
-    let sig = repo.signature().unwrap();
-    let parent = repo.refname_to_id("refs/heads/master").unwrap();
-    let parent = repo.find_commit(parent).unwrap();
-    repo.commit(Some("HEAD"), &sig, &sig,
-                "Another commit", &tree,
-                [&parent]).unwrap();
+    r::init();
 }
 
 test!(simple {
@@ -93,33 +19,35 @@ test!(simple {
             authors = []
 
             [dependencies]
-            foo = ">= 0.0.0"
+            bar = ">= 0.0.0"
         "#)
         .file("src/main.rs", "fn main() {}");
 
+    r::mock_pkg("bar", "0.0.1", []);
+
     assert_that(p.cargo_process("build"),
                 execs().with_status(0).with_stdout(format!("\
 {updating} registry `{reg}`
-{downloading} foo v0.0.1 (the package registry)
-{compiling} foo v0.0.1 (the package registry)
+{downloading} bar v0.0.1 (the package registry)
+{compiling} bar v0.0.1 (the package registry)
 {compiling} foo v0.0.1 ({dir})
 ",
         updating = UPDATING,
         downloading = DOWNLOADING,
         compiling = COMPILING,
         dir = p.url(),
-        reg = registry()).as_slice()));
+        reg = r::registry()).as_slice()));
 
     // Don't download a second time
     assert_that(p.cargo_process("build"),
                 execs().with_status(0).with_stdout(format!("\
 {updating} registry `{reg}`
-[..] foo v0.0.1 (the package registry)
+[..] bar v0.0.1 (the package registry)
 [..] foo v0.0.1 ({dir})
 ",
         updating = UPDATING,
         dir = p.url(),
-        reg = registry()).as_slice()));
+        reg = r::registry()).as_slice()));
 })
 
 test!(deps {
@@ -135,12 +63,15 @@ test!(deps {
         "#)
         .file("src/main.rs", "fn main() {}");
 
+    r::mock_pkg("baz", "0.0.1", []);
+    r::mock_pkg("bar", "0.0.1", [("baz", "*")]);
+
     assert_that(p.cargo_process("build"),
                 execs().with_status(0).with_stdout(format!("\
 {updating} registry `{reg}`
 {downloading} [..] v0.0.1 (the package registry)
 {downloading} [..] v0.0.1 (the package registry)
-{compiling} foo v0.0.1 (the package registry)
+{compiling} baz v0.0.1 (the package registry)
 {compiling} bar v0.0.1 (the package registry)
 {compiling} foo v0.0.1 ({dir})
 ",
@@ -148,7 +79,7 @@ test!(deps {
         downloading = DOWNLOADING,
         compiling = COMPILING,
         dir = p.url(),
-        reg = registry()).as_slice()));
+        reg = r::registry()).as_slice()));
 })
 
 test!(nonexistent {
@@ -185,6 +116,9 @@ test!(bad_cksum {
         "#)
         .file("src/main.rs", "fn main() {}");
 
+    r::mock_pkg("bad-cksum", "0.0.1", []);
+    File::create(&r::mock_archive_dst("bad-cksum", "0.0.1")).unwrap();
+
     assert_that(p.cargo_process("build").arg("-v"),
                 execs().with_status(101).with_stderr("\
 Unable to get packages from source
@@ -217,7 +151,7 @@ location searched: the package registry
 version required: >= 0.0.0
 "));
 
-    publish_notyet();
+    r::mock_pkg("notyet", "0.0.1", []);
 
     assert_that(p.process(cargo_dir().join("cargo")).arg("build"),
                 execs().with_status(0).with_stdout(format!("\
@@ -230,7 +164,7 @@ version required: >= 0.0.0
         downloading = DOWNLOADING,
         compiling = COMPILING,
         dir = p.url(),
-        reg = registry()).as_slice()));
+        reg = r::registry()).as_slice()));
 })
 
 test!(package_with_path_deps {
@@ -265,7 +199,7 @@ location searched: the package registry
 version required: ^0.0.1
 "));
 
-    publish_notyet();
+    r::mock_pkg("notyet", "0.0.1", []);
 
     assert_that(p.process(cargo_dir().join("cargo")).arg("package"),
                 execs().with_status(0).with_stdout(format!("\